home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue100 / CPROG12.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  1.2 KB  |  42 lines

  1. /* CPROG12.CPP  - Set up and use a structure tag */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. //----------------------------------------------------------------
  7.  
  8. struct person { int age;
  9.         char name[30];
  10.           };
  11.  
  12. /* This doesn't create a structure - it just defines a template, called
  13. person, that may be used to make strcutures in this format. Person is a
  14. structure tag. We could, if we wanted to, create actual structures in
  15. this declaration by naming them between the closing brace and the ; */
  16.  
  17. //----------------------------------------------------------------
  18. void afunction(void)
  19. {
  20. struct person doris;
  21.  
  22.     doris.age=73;
  23.     strcpy( doris.name, "Doris Smith");
  24.     printf("Name: %s      Age: %d\n", doris.name, doris.age);
  25. }
  26.  
  27. /* Doris is a structure of type person. Since she is volatile, her components
  28. must be initialised with explicit program instructions. */
  29.  
  30. //----------------------------------------------------------------
  31.  
  32. void main(void)
  33. {
  34. static struct person fred = {30, "Fred Jones"};
  35.  
  36.     afunction();
  37.     printf("Name: %s       Age: %d\n", fred.name, fred.age);
  38. }
  39.  
  40. /* Fred is another strcuture of type person. Because he is static, he can
  41. be initialised during compilation. */
  42.